<html>
<head><meta charset="utf-8"><title>Ternary bit match pattern · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ternary.20bit.20match.20pattern.html">Ternary bit match pattern</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="209540872"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ternary%20bit%20match%20pattern/near/209540872" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Braden Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ternary.20bit.20match.20pattern.html#209540872">(Sep 09 2020 at 17:18)</a>:</h4>
<p>For bit level parsing tasks, the ability to selectively match and ignore individual bits of a integer could be quite helpful. For example, using the (undecided) syntax “0t...”</p>
<div class="codehilite"><pre><span></span><code><span class="mi">0</span><span class="n">t10</span><span class="o">--</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="o">..</span><span class="p">.</span><span class="w"> </span><span class="c1">//Hits ..01000, ..01001, ..01010, and ..01011</span>
<span class="mi">0</span><span class="n">t11xx</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="c1">//Matches 11--, puts ignored part in x. Would need some serious work to fit this in syntactically!</span>
<span class="mi">0</span><span class="n">t1</span><span class="o">-</span><span class="mi">11</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="o">..</span><span class="p">.</span><span class="w"> </span><span class="c1">// matches 1011 and 1111.</span>
<span class="mi">0</span><span class="n">t1</span><span class="o">-</span><span class="mi">0</span><span class="o">-</span><span class="mi">0</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="o">..</span><span class="p">.</span><span class="w"> </span><span class="c1">// matches 10000, 10010, 11000, and 11010.</span>
<span class="o">..</span><span class="p">.</span><span class="w"></span>
</code></pre></div>


<p>Was hoping for some quick discussion on the merits of the concept before taking it to the forum. Note: patterns that do not match the full type require the undeclared bits be 0. 0t10-- Doesn’t match 11000.</p>



<a name="209541407"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ternary%20bit%20match%20pattern/near/209541407" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ternary.20bit.20match.20pattern.html#209541407">(Sep 09 2020 at 17:22)</a>:</h4>
<p><span class="user-mention" data-user-id="277614">@Braden Nelson</span> I think it's been discussed a couple of times before as well, and I believe there's a crate providing a macro for that, too.</p>



<a name="209541435"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ternary%20bit%20match%20pattern/near/209541435" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ternary.20bit.20match.20pattern.html#209541435">(Sep 09 2020 at 17:22)</a>:</h4>
<p>It seems potentially reasonable.</p>



<a name="209541447"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ternary%20bit%20match%20pattern/near/209541447" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ternary.20bit.20match.20pattern.html#209541447">(Sep 09 2020 at 17:22)</a>:</h4>
<p>Two notable issues:</p>



<a name="209541487"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ternary%20bit%20match%20pattern/near/209541487" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ternary.20bit.20match.20pattern.html#209541487">(Sep 09 2020 at 17:22)</a>:</h4>
<ul>
<li>Syntax, which seems likely to be endlessly bikesheddable.</li>
</ul>



<a name="209541540"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ternary%20bit%20match%20pattern/near/209541540" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Braden Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ternary.20bit.20match.20pattern.html#209541540">(Sep 09 2020 at 17:23)</a>:</h4>
<p>I see a big merit to putting it into the language directly: optimization. Compiler can do a lot of transforms a macro cannot.</p>



<a name="209541601"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ternary%20bit%20match%20pattern/near/209541601" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ternary.20bit.20match.20pattern.html#209541601">(Sep 09 2020 at 17:23)</a>:</h4>
<ul>
<li>Ensuring the match arms provide complete coverage of possible values.</li>
</ul>



<a name="209541666"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ternary%20bit%20match%20pattern/near/209541666" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ternary.20bit.20match.20pattern.html#209541666">(Sep 09 2020 at 17:23)</a>:</h4>
<p>Regarding syntax, I wouldn't want to use <code>-</code> because it looks like subtraction. And <code>_</code> is already used for spacing within numbers.</p>



<a name="209541747"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ternary%20bit%20match%20pattern/near/209541747" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ternary.20bit.20match.20pattern.html#209541747">(Sep 09 2020 at 17:24)</a>:</h4>
<p>I do like the idea of being able to match out specific bits.</p>



<a name="209541775"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ternary%20bit%20match%20pattern/near/209541775" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ternary.20bit.20match.20pattern.html#209541775">(Sep 09 2020 at 17:24)</a>:</h4>
<p>On a slightly related note, which might be able to share some support with the match arm checker:</p>



<a name="209541789"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ternary%20bit%20match%20pattern/near/209541789" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Braden Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ternary.20bit.20match.20pattern.html#209541789">(Sep 09 2020 at 17:24)</a>:</h4>
<p>The coverage problem is actually not too hard. You can collapse these bit patterns down, but it may be expensive to calculate in some cases (where there’s lots of holes in coverage)</p>



<a name="209541885"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ternary%20bit%20match%20pattern/near/209541885" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Braden Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ternary.20bit.20match.20pattern.html#209541885">(Sep 09 2020 at 17:25)</a>:</h4>
<p>Would need to support multiple ranges of values being covered in one arm.</p>



<a name="209542040"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ternary%20bit%20match%20pattern/near/209542040" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ternary.20bit.20match.20pattern.html#209542040">(Sep 09 2020 at 17:26)</a>:</h4>
<p>It'd be nice to be able to write this and have Rust accept it as complete:</p>
<div class="codehilite"><pre><span></span><code><span class="k">fn</span> <span class="nf">func</span><span class="p">(</span><span class="n">x</span>: <span class="kt">u8</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="o">..</span><span class="p">.</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">match</span><span class="w"> </span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="mi">5</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="mi">0</span><span class="w"> </span>-&gt; <span class="o">..</span><span class="p">.</span><span class="w"></span>
<span class="w">        </span><span class="mi">1</span><span class="w"> </span>-&gt; <span class="o">..</span><span class="p">.</span><span class="w"></span>
<span class="w">        </span><span class="mi">2</span><span class="w"> </span>-&gt; <span class="o">..</span><span class="p">.</span><span class="w"></span>
<span class="w">        </span><span class="mi">3</span><span class="w"> </span>-&gt; <span class="o">..</span><span class="p">.</span><span class="w"></span>
<span class="w">        </span><span class="mi">4</span><span class="w"> </span>-&gt; <span class="o">..</span><span class="p">.</span><span class="w"></span>
<span class="w">        </span><span class="mi">5</span><span class="w"> </span>-&gt; <span class="o">..</span><span class="p">.</span><span class="w"></span>
<span class="w">        </span><span class="mi">6</span><span class="w"> </span>-&gt; <span class="o">..</span><span class="p">.</span><span class="w"></span>
<span class="w">        </span><span class="mi">7</span><span class="w"> </span>-&gt; <span class="o">..</span><span class="p">.</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="o">..</span><span class="p">.</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="209542314"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ternary%20bit%20match%20pattern/near/209542314" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Braden Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ternary.20bit.20match.20pattern.html#209542314">(Sep 09 2020 at 17:28)</a>:</h4>
<p>Indeed it would. That’s probably the more complicated part, but it’s still decidable. Adding it could make certain compiler optimizations possible and probably  improve match ergonomics in other cases. Problem is that it’s extra data to carry around with the type and can get quite complex.</p>



<a name="209542447"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ternary%20bit%20match%20pattern/near/209542447" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Braden Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ternary.20bit.20match.20pattern.html#209542447">(Sep 09 2020 at 17:29)</a>:</h4>
<p>LLVM has some infrastructure to do this on their end already iirc (it can figure out the range of, say, <code>bool1 &lt;&lt; 1 | bool2</code>)</p>



<a name="209542527"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ternary%20bit%20match%20pattern/near/209542527" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Braden Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ternary.20bit.20match.20pattern.html#209542527">(Sep 09 2020 at 17:29)</a>:</h4>
<p>Some assumptions in that comment, do correct me if I’m wrong.</p>



<a name="209543430"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ternary%20bit%20match%20pattern/near/209543430" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ternary.20bit.20match.20pattern.html#209543430">(Sep 09 2020 at 17:36)</a>:</h4>
<p>Yeah, LLVM has some range infrastructure.</p>



<a name="209543550"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ternary%20bit%20match%20pattern/near/209543550" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ternary.20bit.20match.20pattern.html#209543550">(Sep 09 2020 at 17:37)</a>:</h4>
<p>I don't know if that's early enough for us to use it though.</p>



<a name="209543551"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ternary%20bit%20match%20pattern/near/209543551" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ternary.20bit.20match.20pattern.html#209543551">(Sep 09 2020 at 17:37)</a>:</h4>
<p>We may need to handle that ourselves.</p>



<a name="209543603"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ternary%20bit%20match%20pattern/near/209543603" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Braden Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ternary.20bit.20match.20pattern.html#209543603">(Sep 09 2020 at 17:37)</a>:</h4>
<p>That’s very likely. I only mentioned it as it’d be a helpful reference.</p>



<a name="209544480"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ternary%20bit%20match%20pattern/near/209544480" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Braden Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ternary.20bit.20match.20pattern.html#209544480">(Sep 09 2020 at 17:43)</a>:</h4>
<p>A potentially helpful side effect of range detection in the compiler would be rustc being able to omit overflow checks on it’s own instead of relying on LLVM. And a whole class of check omission optimizations.</p>



<a name="209545418"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ternary%20bit%20match%20pattern/near/209545418" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ternary.20bit.20match.20pattern.html#209545418">(Sep 09 2020 at 17:51)</a>:</h4>
<p>LLVM is pretty good at that already</p>



<a name="209546494"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ternary%20bit%20match%20pattern/near/209546494" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ternary.20bit.20match.20pattern.html#209546494">(Sep 09 2020 at 18:00)</a>:</h4>
<p>It is, but having that in Rust tends to be faster, and we might be able to do some kinds of range checking pre-monomorphization.</p>



<a name="209565501"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ternary%20bit%20match%20pattern/near/209565501" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ternary.20bit.20match.20pattern.html#209565501">(Sep 09 2020 at 20:34)</a>:</h4>
<p>I think that to make <code>match x &amp; 1 { 0 =&gt; ..., 1 =&gt; ... }</code> legal I'd want there to be an actual type that came out of the <code>&amp;</code>.  (Like the <code>Integer&lt;LOW, HIGH&gt;</code> that's been oft discussed.)</p>



<a name="209568991"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ternary%20bit%20match%20pattern/near/209568991" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ternary.20bit.20match.20pattern.html#209568991">(Sep 09 2020 at 21:09)</a>:</h4>
<p><span class="user-mention" data-user-id="125270">@scottmcm</span> That type won't always be expressible in the type system, though.</p>



<a name="209569025"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ternary%20bit%20match%20pattern/near/209569025" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ternary.20bit.20match.20pattern.html#209569025">(Sep 09 2020 at 21:09)</a>:</h4>
<p><span class="user-mention" data-user-id="125270">@scottmcm</span> Or rather, what the Rust compiler is capable of proving many not be.</p>



<a name="209569154"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ternary%20bit%20match%20pattern/near/209569154" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ternary.20bit.20match.20pattern.html#209569154">(Sep 09 2020 at 21:10)</a>:</h4>
<p>And we may not want to expose that information in the user-visible type system.</p>



<a name="209570766"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Ternary%20bit%20match%20pattern/near/209570766" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Ternary.20bit.20match.20pattern.html#209570766">(Sep 09 2020 at 21:25)</a>:</h4>
<p>Right, but we need a stability guarantee for match checking.  And we could certainly prove more things than we might want to promise (like the discussions about using SAT solvers to do things).</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>